﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using EnvDTE;
using Microsoft.VisualStudio.TemplateWizard;
using Debugger = System.Diagnostics.Debugger;

namespace Creventive.Wpf.ShellFactory.TemplateWizard
{
    public class ViewTemplateWizard : IWizard
    {
        private Dictionary<string, string> replacementsDictionary;

        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            this.replacementsDictionary = replacementsDictionary;
            string rootName = this.replacementsDictionary["$rootname$"];
            string safeRootItemName = Regex.Replace(Path.GetFileNameWithoutExtension(rootName), "[^a-zA-Z_0-9]", "_");
            if (Regex.IsMatch(safeRootItemName, @"\A[0-9]"))
                safeRootItemName = '_' + safeRootItemName.Substring(1);
            this.replacementsDictionary["$saferootitemname$"] = safeRootItemName;
        }

        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }

        public void RunFinished()
        {
        }

        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }

        public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
        }

        public void ProjectFinishedGenerating(Project project)
        {
        }
    }
}
